
//
//  AppDelegate.swift
//  sinaDemo
//
//  Created by mac on 15/12/12.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        addNotificationObserver()
        // 创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // 取出账户信息,是否已经存在,如果存在,那么直接进入主页
        // 初始化window的根控制器
        window?.rootViewController = defaultViewController()
//        window?.rootViewController = UINavigationController(rootViewController: ComposeController())
//        window?.rootViewController = ComposePictureController()
        window?.makeKeyAndVisible()
        setUpTintColor()
        return true
    }
    
    // MARK: 添加通知观察者
    private func addNotificationObserver() {
        notificationCenter.addObserver(self, selector: "switchVC:", name: switchRootVCNotification, object: nil)
    }
    
    // 在swift中只要是target selector 消息机制的需要在方法前面加上一个@objc
    /*
    1.按钮的点击事件
    2.代理的可选方法
    3.通知的调用
    */
    @objc private func switchVC(notifi: NSNotification) {
        // 首先判断用户是否已经登录,没有登录到登录界面,然后到欢迎界面,如果登录过了,判断是否新版本,是现实新特性,不是欢迎界面,最后到首页
        if notifi.object != nil { // 跳转到欢迎界面
            window?.rootViewController = WelcomeViewController()
            return
        }
        window?.rootViewController = MainTabBarController()
    }
    
    private func defaultViewController() ->UIViewController {
        if isNewVersion() { // 是新版本,现实新特性
            return NewFeatureViewController()
        }
        // 不是新特性,显示欢迎界面
        return WelcomeViewController()
    }
    
    // 判断是否是新特性
    private func isNewVersion() ->(Bool) {
        // 取出当前的版本
        let versionStr = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let versionNum = Double(versionStr)
        
        // 取出上次的版本
        guard let lastVersion: Double = NSUserDefaults.standardUserDefaults().doubleForKey("CFBundleShortVersionString") else {
            return false
        }
        // 有上次的版本存储,立即存储这次的版本
        NSUserDefaults.standardUserDefaults().setDouble(versionNum!, forKey: "CFBundleShortVersionString")
        // 同步
        NSUserDefaults.standardUserDefaults().synchronize()
        
        // 不相同就是新版本
        return versionNum! > lastVersion
    }
    
    // MARK: 移除通知,要有这个意识,但是在这里不需要移除
    deinit {
        notificationCenter.removeObserver(self)
    }
    
    // MARK: 设置导航栏和tabbar的主题颜色
    private func setUpTintColor() {
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

